package com.kuang.config;

import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class UserInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        HttpSession session = request.getSession();
        //放行: 判断什么情况下没有登录

        //登录页面也会放行
        if (request.getRequestURI().contains("login")){
            return true;
        }

        //请求放行
        if (request.getRequestURI().contains("check")){
            return true;
        }

        //第一次登录也是没有session的
        if (session.getAttribute("user_code")!=null){
            return true;
        }

        //判断什么情况下没有登录
        request.getRequestDispatcher("/index.jsp").forward(request,response);
        return  false;

    }
}
